{#
This file is part of EC-CUBE

Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.

http://www.ec-cube.co.jp/

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#}
{% extends 'default_frame.twig' %}

{% set menus = ['product', 'class_name'] %}

{% block title %}商品管理{% endblock %}
{% block sub_title %}規格編集{% endblock %}

{% form_theme form 'Form/bootstrap_3_horizontal_layout.html.twig' %}

{% block javascript %}
    <script src="{{ app.config.admin_urlpath }}/assets/js/vendor/jquery.ui/jquery.ui.core.min.js"></script>
    <script src="{{ app.config.admin_urlpath }}/assets/js/vendor/jquery.ui/jquery.ui.widget.min.js"></script>
    <script src="{{ app.config.admin_urlpath }}/assets/js/vendor/jquery.ui/jquery.ui.mouse.min.js"></script>
    <script src="{{ app.config.admin_urlpath }}/assets/js/vendor/jquery.ui/jquery.ui.sortable.min.js"></script>
    <script>
        $(function() {
            var oldRanks = [];
            // 画面の中のrank一覧を保持
            $("div.tableish > .item_box").each(function() {
                oldRanks.push(this.dataset.rank);
            });
            // rsort
            oldRanks.sort(function(a,b){return a - b;}).reverse();

            $("div.tableish").sortable({
                items: '> .item_box',
                cursor: 'move',
                update: function(e, ui) {
                    $("body").append($('<div class="modal-backdrop in"></div>'));
                    updateRank();
                }
            });

            var updateRank = function() {
                // 並び替え後にrankを更新
                var newRanks = {};
                var i = 0;
                $("div.tableish > .item_box").each(function() {
                    newRanks[this.dataset.classNameId] = oldRanks[i];
                    i++;
                });

                $.ajax({
                    url: '{{ url('admin_product_class_name_rank_move') }}',
                    type: 'POST',
                    data: newRanks,
                }).done(function(data) {
                    console.log(data);
                    $(".modal-backdrop").remove();
                }).fail(function() {
                    console.log('fail');
                    $(".modal-backdrop").remove();
                });
            };
        });
    </script>
{% endblock %}

{% block main %}
    <div id="edit_wrap" class="row">
        <div id="edit_list_box" class="col-md-12">
            <div id="edit_list_box__body" class="box">
                <div id="edit_box" class="box-header">
                    <form role="form" class="form-horizontal" name="form1" id="form1" method="post" action="{% if TargetClassName.id %}{{ path('admin_product_class_name_edit', {id: TargetClassName.id}) }}{% else %}{{ url('admin_product_class_name') }}{% endif %}">
                        <div id="edit_box__body" class="form-group">
                            <div class="col-md-12 form-inline">
                                {{ form_widget(form._token) }}
                                {{ form_widget(form.name, {attr: {placeholder: '規格名を入力'}}) }}
                                {{ form_errors(form.name) }}
                                <button class="btn btn-default btn-sm" type="submit">規格作成</button>
                            </div>
                        </div>
                        <div class="extra-form">
                            {% for f in form.getIterator %}
                                {% if f.vars.name matches '[^plg*]' %}
                                    {{ form_label(f) }}
                                    {{ form_widget(f) }}
                                    {{ form_errors(f) }}
                                {% endif %}
                            {% endfor %}
                        </div>
                    </form>
                </div><!-- /.box-header -->
                {% if ClassNames|length > 0 %}
                    <div id="sortable_list_box" class="box-body no-padding no-border">
                        <div id="list_box" class="sortable_list">
                            <div id="sortable_list_box__body" class="tableish">

                                {% for ClassName in ClassNames %}

                                    <div id="sortable_list_box__item--{{ ClassName.id }}" class="item_box tr" data-class-name-id="{{ ClassName.id }}" data-rank="{{ ClassName.rank }}">
                                        <div class="icon_sortable td">
                                            <svg class="cb cb-ellipsis-v"> <use xlink:href="#cb-ellipsis-v" /></svg>
                                        </div>
                                        <div id="sortable_list_box__name--{{ ClassName.id }}" class="item_pattern td">
                                            <a href="{{ url('admin_product_class_category', {class_name_id : ClassName.id }) }}">
                                                {{ ClassName.name }} ({{ ClassName.ClassCategories|length }})
                                            </a>
                                        </div>
                                        <div id="sortable_list_box__menu_box--{{ ClassName.id }}" class="icon_edit td">
                                            <div id="sortable_list_box__menu_box_toggle--{{ ClassName.id }}" class="dropdown">
                                                <a class="dropdown-toggle" data-toggle="dropdown"><svg class="cb cb-ellipsis-h"> <use xlink:href="#cb-ellipsis-h" /></svg></a>
                                                <ul id="sortable_list_box__menu--{{ ClassName.id }}" class="dropdown-menu dropdown-menu-right">
                                                    <li>
                                                        <a href="{{ url('admin_product_class_category', {class_name_id : ClassName.id }) }}">
                                                            分類登録
                                                        </a>
                                                    </li>

                                                    {% if ClassName.id != TargetClassName.id %}
                                                        <li><a href="{{ url('admin_product_class_name_edit', {id: ClassName.id}) }}">編集</a></li>
                                                    {% else %}
                                                        <li><a>編集中</a></li>
                                                    {% endif %}

                                                    {% if ClassName.ClassCategories|length == 0 %}
                                                        <li>
                                                            <a href="{{ url('admin_product_class_name_delete', {id: ClassName.id}) }}" {{ csrf_token_for_anchor() }} data-method="delete">
                                                                削除
                                                            </a>
                                                        </li>
                                                    {% endif %}

                                                </ul>
                                            </div>
                                        </div>
                                    </div><!-- /.item_box -->
                                {% endfor %}

                            </div>
                        </div>
                    </div><!-- /.box-body -->
                {% else %}
                    <div id="sortable_list_box" class="box-body no-padding">
                        <div id="sortable_list_box__not_find_message" class="data-empty"><svg class="cb cb-inbox"> <use xlink:href="#cb-inbox" /></svg><p>データはありません</p></div>
                    </div><!-- /.box-body -->
                {% endif %}
                <!-- ▲ データがある時 ▲ -->
            </div><!-- /.box -->
        </div><!-- /.col -->
    </div>
{% endblock %}
